/**
 * 更新foods store中的mockFoods数据
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 读取生成的食物数据
const generatedDataPath = path.join(__dirname, '../src/stores/foods-data-generated.ts');
const generatedData = fs.readFileSync(generatedDataPath, 'utf8');

// 读取food_data.json获取实际食物数量
const foodDataPath = path.join(__dirname, '../database/food_data.json');
const foodData = JSON.parse(fs.readFileSync(foodDataPath, 'utf8'));
const foodCount = foodData.length;

// 提取mockFoods数组内容（从第2行到倒数第1行）
const lines = generatedData.split('\n');
const mockFoodsContent = lines.slice(1).join('\n'); // 跳过第一行注释

// 读取当前的foods.ts
const foodsStorePath = path.join(__dirname, '../src/stores/foods.ts');
const foodsStoreContent = fs.readFileSync(foodsStorePath, 'utf8');

// 找到mockFoods数组的开始和结束位置
const startMarker = 'const mockFoods: Partial<Food>[] = [';
const endMarker = ']';

const startIndex = foodsStoreContent.indexOf(startMarker);
if (startIndex === -1) {
  console.error('找不到mockFoods数组的开始标记');
  process.exit(1);
}

// 找到对应的结束括号（在export之前）
const exportIndex = foodsStoreContent.indexOf('export const useFoodsStore');
const endIndex = foodsStoreContent.lastIndexOf(endMarker, exportIndex);

if (endIndex === -1) {
  console.error('找不到mockFoods数组的结束标记');
  process.exit(1);
}

// 构建新的文件内容
const beforeMockFoods = foodsStoreContent.substring(0, startIndex);
const afterMockFoods = foodsStoreContent.substring(endIndex + 1);

const newContent = beforeMockFoods + mockFoodsContent + afterMockFoods;

// 写入新文件
fs.writeFileSync(foodsStorePath, newContent, 'utf8');

console.log('✅ 成功更新 src/stores/foods.ts');
console.log(`   - 更新了 ${foodCount} 种食物数据`);

